﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

public class StaticIni : BaseObject
{
    private static StaticIni self;
    public StaticIni() { }

    public static StaticIni getInstance()
    {
        if (self == null)
        {
            self = new StaticIni();
        }
        return self;
    }

    /// <summary>
    /// 读取ini配置文件到内存
    /// </summary>
    public void LoadINI(string path)
    {
        LoadINI(path, Encoding.UTF8);
    }
    public void LoadINI(string path, Encoding type)
    {
        StreamReader sr = new StreamReader(path, type);

        string mainStr = sr.ReadToEnd();

        string[] splitStr = mainStr.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries);

        foreach (string val in splitStr)
        {
            string[] eachStr = val.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            string firstStr = eachStr[0];
            firstStr = firstStr.Trim(new char[] { '[', ']' });

            List<string> ls = new List<string>();
            ls.AddRange(eachStr);
            ls.RemoveAt(0);

            Hashtable ht = new Hashtable();
            foreach (string eachVal in ls)
            {
                string[] spStr = eachVal.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                ht.Add(spStr[0], spStr[1]);
            }
            HandleProperties(firstStr, ht);
        }
    }

    /// <summary>
    /// 获取ini配置对应key下的键值对表
    /// </summary>
    public Hashtable getIniObj(string configID)
    {
        if (CheckProperties(configID) == false) return null;

        return (Hashtable)GetProperties(configID);
    }

    /// <summary>
    /// 获取ini配置文件key下的键对应的值
    /// </summary>
    public string getIniVar(string configID, string valKey)
    {
        if (CheckProperties(configID) == false) return "";

        Hashtable ht = (Hashtable)GetProperties(configID);
        if(ht.ContainsKey(valKey))
        {
            return (string)ht[valKey];
        }
        return "";

    }


}